/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.feng.newconfig.nacosdiscoveryprovider;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.nacos.common.utils.ExceptionUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EchoServiceController {

    @GetMapping("/echo/{message}")
    public String echo(@PathVariable String message) {
        return "[ECHO] : " + message;
    }

    @GetMapping("/slow")
//    @SentinelResource(value = "slow", fallback = "fallbackHandler")
    // 对应的 `slow` 函数需要位于 `SlowFallback` 类中，并且必须为 static 函数
//    @SentinelResource(value = "slow", fallback = "slow", fallbackClass = SlowFallback.class)
    // 对应的 `slow` 函数需要位于 `SlowFallback` 类中，并且必须为 static 函数
//    @SentinelResource(value = "slow", blockHandler = "slow", blockHandlerClass = {SlowFallback.class})
//    @SentinelResource(value = "slow", blockHandler = "blockHandler", fallback = "fallbackHandlerThrowable")
//    @SentinelResource(value = "slow", defaultFallback = "defaultFallback", exceptionsToIgnore = {InterruptedException.class})
    public String slow() throws InterruptedException {
        Thread.sleep(1000);
        return "8088";
    }

    /**
     * 自定义熔断异常
     * 返回值和参数要跟目标函数一样
     */
    public String fallbackHandler() {
        return "触发降级";
    }

    // 限流：参数最后多一个 BlockException，其余与原函数一致.
    public String blockHandler(BlockException ex) {
        return "触发限流";
    }

    // 降级：函数签名与原函数一致或加一个 Throwable 类型的参数.
    public String fallbackHandlerThrowable(Throwable ex) {
        return "fallbackHandlerThrowable";
    }

    //默认的 fallback 函数名称
    public String defaultFallback() {
        return "default_fallback";
    }

}
